
import Api, { GET_CART, GET_KEY, GOODS_SELECT, UPDATE_GOODS_NUM, REMOVE_CART,SELECT_ALL} from '../../utils/api.js';
import { handPic,getUrl} from '../../utils/util.js';


const App = getApp()
Page({
    data: {
        goods_list:{},
        cart_data: {},
        goods_status: [],
        clear_all: false,
        key:'',
        ctype:'',
        total_price:0,
        select_all:false
    },
    checkSelect:function(goods){
        let goods_count = goods.length,
            select_count = 0;
        goods.map((v,k)=>{
            if(v.selected == 1){
                select_count += 1;
            }
        })


        this.setData({
            select_all:goods_count == select_count?true:false
        })
    },
    // 修改商品状态
    modify: function (e) {
        const type = e.target.dataset.type;
        const idx = e.target.dataset.index;
        const { goods_list,clear_all,key,ctype} = this.data;
        let cid = goods_list[idx].id;
        if (type == "checked") {
            //  勾选
            let select = goods_list[idx].selected = goods_list[idx].selected==1?0:1;
            this.checkSelect(goods_list);
            Api.fetch(GOODS_SELECT,{key,key,cid:cid,select:select,ctype:ctype},resp =>{
                this.setData({
                    goods_list:goods_list,
                    total_price:resp.total_price,
                    discount_price:resp.discount_price
                });
            })
        } else {
            //  数量

            let operator = '';
            
            if (type == "minus") {
                operator = 'sub';
                if(goods_list[idx].goods_num <= 1){
                    return;
                }
                goods_list[idx].goods_num -= 1;
            } else if (type == "add") {
                if (goods_list[idx].goods_num >= goods_list[idx].store) {
                    return;
                }
                goods_list[idx].goods_num = parseInt(goods_list[idx].goods_num)+  parseInt(1);
                operator = 'add';
            }

            Api.fetch(UPDATE_GOODS_NUM, { key: key, cid: cid, ctype: ctype, operator: operator }, resp => {
                
                this.setData({
                    total_price: resp.total_price,
                    discount_price: resp.discount_price,
                    goods_list:goods_list
                })
            })

        }
    },
    // 删除购物车数据   
    del: function (e) {
        let _this = this;
        const idx = e.target.dataset.index;
        const { goods_list,key} = this.data;
        wx.showModal({
            title: '确定要删除该商品吗',
            success: function (res) {
                if (res.confirm) {
                    Api.fetch(REMOVE_CART,{key:key,cid:goods_list[idx].id},resp =>{
                        goods_list.splice(idx, 1);
                        _this.setData({ goods_list: goods_list })
                    })
                }
            }
        })
    },
    // 清空购物车
    selectAll: function (e) {
        const {key,ctype,select_all} = this.data;

        Api.fetch(SELECT_ALL,{key:key,ctype:ctype,select_all:select_all?0:1},resp=>{
            resp.list.map((v, k) => {
                v.goods_img = handPic(v.goods_img, 4);
            })
            this.checkSelect(resp.list);
            this.setData({
                goods_list: resp.list,
                ctype: ctype,
                total_price: resp.total_price
            })
        })
    },

    // 通用页面跳转
    navigateTo: function (e) {
        const tip = e.target.dataset.tip;
        const gc_id = e.target.dataset.gc_id;
        const {ctype} = this.data;
        let url;

        switch(tip){
            case 'goods_detail' :
                url = getUrl(tip, { gc_id: gc_id });
                App.navigateTo(url);
            break;
            case 'pay':
                url = getUrl(tip,{ctype:ctype});
                App.navigateTo(url);
            break;
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _this = this,
            key = '';

        //判断是否登录
        if (Api.checkLogin(function () {
            _this.onLoad(options);
            _this.onShow();
        })) {
            let ctype = options.type ? options.type : 'normal';
            
            key = Api.getKey();
            this.setData({
                key: key,
                ctype:ctype
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let {key,ctype} = this.data;
        Api.fetch(GET_CART, { key: key, ctype: ctype }, resp => {
            resp.list.map((v, k) => {
                v.goods_img = handPic(v.goods_img, 4);
            })
            this.checkSelect(resp.list);
            this.setData({
                goods_list: resp.list,
                ctype: ctype,
                total_price: resp.total_price
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})